﻿Option Strict On

Imports DbDataLib
Imports System.Data.Common
Imports log4net
Imports KUeLib.Data.ServiceDefinitions

Public Class ServiceManager

#Region "Singleton"

    Private Shared _instance As ServiceManager

    Public Shared Function GetInstance() As ServiceManager
        If _instance Is Nothing Then _
            _instance = New ServiceManager()
        Return _instance
    End Function

#End Region

    'Private Const CONNECTION_NAME As String = "kostenuebersicht"
    ' Private Const PROVIDER_NAME As String = "MySql.Data.MySqlClient"
    ' Private Const CONNECTION_STRING As String = "Server=localhost;Database=kostenuebersicht;UserId=root;"
    'Private Const CONNECTION_LIB As String = "E:\Entwicklung\ClientServer.NET\MultiPlug\KUeLib.Data.SQLite\bin\Debug\KUeLib.Data.SQLite.dll"
    'Private Const BUCHUNGEN_SERVICE_TYPE As String = "KUeLib.Data.SQLite.Services.BuchungenDataSerivce"
    'Private Const BUCHUNGEN_POS_SERVICE_TYPE As String = "KUeLib.Data.SQLite.Services.BuchungspositionenDataService"
    'Private Const FILE_ATT_TYPE As String = "KUeLib.Data.SQLite.Services.FileAttachmentDataService"
    'Private Const KATEGORIEN_TYPE As String = "KUeLib.Data.SQLite.Services.KategorienDataService"
    'Private Const KTO_AUSZUG_SERVICE_TYPE As String = "KUeLib.Data.SQLite.Services.KontoauszugDataService"
    'Private Const STATUS_DATE_SERVICE_TYPE As String = "KUeLib.Data.SQLite.Services.StatusDataService"
    'Private Const VERTRAG_SERVICE_TYPE As String = "KUeLib.Data.SQLite.Services.VertragsDataService"

    'Private Const CONNECTION_LIB As String = "E:\Entwicklung\ClientServer.NET\MultiPlug\KUeDataLib\bin\Debug\KUeLib.Data.MySql.dll"
    'Private Const BUCHUNGEN_SERVICE_TYPE As String = "KUeLib.Data.MySql.Services.BuchungenDataSerivce"
    'Private Const BUCHUNGEN_POS_SERVICE_TYPE As String = "KUeLib.Data.MySql.Services.BuchungspositionenDataService"
    'Private Const FILE_ATT_TYPE As String = "KUeLib.Data.MySql.Services.FileAttachmentDataService"
    'Private Const KATEGORIEN_TYPE As String = "KUeLib.Data.MySql.Services.KategorienDataService"
    'Private Const KTO_AUSZUG_SERVICE_TYPE As String = "KUeLib.Data.MySql.Services.KontoauszugDataService"
    'Private Const STATUS_DATE_SERVICE_TYPE As String = "KUeLib.Data.MySql.Services.StatusDataService"
    'Private Const VERTRAG_SERVICE_TYPE As String = "KUeLib.Data.MySql.Services.VertragsDataService"

    Private _ilog As ILog = log4net.LogManager.GetLogger(GetType(ServiceManager))
    Private _dataService As DbDataService
    Private _serviceAssembly As System.Reflection.Assembly
    Private _config As Configuration.ServiceConfiguration

    Private Sub New()

        'DbDataRepository.GetInstance().GetDbDataService(CONNECTION_NAME, _
        '                                                CONNECTION_STRING, _
        '                                                PROVIDER_NAME)
        _config = CType(System.Configuration.ConfigurationManager.GetSection("kueLibDataConfig"),  _
        Configuration.ServiceConfiguration)

        _dataService = DbDataRepository.GetInstance.GetDbDataService(_config.ConnectionName, True)
        _serviceAssembly = System.Reflection.Assembly.LoadFrom(_config.ServiceDll)
    End Sub

#Region "Service Instanzen"

    Private _buchungenDataService As BuchungenDataSerivce
    Private _kategorieService As KategorienDataService
    Private _fileAttachmentDataService As FileAttachmentDataService
    Private _buchungspositionenDataService As BuchungspositionenDataService
    Private _vertragsDataService As VertragsDataService
    Private _statusDataService As StatusDataService
    Private _kontoauszugDataService As KontoauszugDataService

#End Region

    Public Function GetDataService(ByVal name As String) As DbDataService
        Return DbDataRepository.GetInstance().GetDbDataService(name, False)
    End Function

#Region "Factory Methods"

    Public Function GetBuchungenDataService() As BuchungenDataSerivce

        If _buchungenDataService Is Nothing Then
            _buchungenDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.BuchungServiceType), New Object() {_dataService}), 
                BuchungenDataSerivce)
        End If

        _ilog.DebugFormat(_buchungenDataService.GetType().ToString() & " abgerufen.")

        Return _buchungenDataService
    End Function

    Public Function GetKategorienDataService() As KategorienDataService

        If _kategorieService Is Nothing Then
            _kategorieService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.KategorienServiceType), New Object() {_dataService}), 
                KategorienDataService)
        End If

        _ilog.DebugFormat(_kategorieService.GetType().ToString() & " abgerufen.")

        Return _kategorieService
    End Function

    Public Function GetFileAttachmentDataService() As FileAttachmentDataService

        If _fileAttachmentDataService Is Nothing Then
            _fileAttachmentDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.FileAttachemntServiceType), New Object() {_dataService}), 
                FileAttachmentDataService)
        End If

        _ilog.DebugFormat(_fileAttachmentDataService.GetType().ToString() & " abgerufen.")

        Return _fileAttachmentDataService
    End Function

    Public Function GetBuchungspositionenDataService() As BuchungspositionenDataService

        If _buchungspositionenDataService Is Nothing Then
            _buchungspositionenDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.BuchungPositionenServiceType), New Object() {_dataService}), 
                BuchungspositionenDataService)
        End If

        _ilog.DebugFormat(_buchungspositionenDataService.GetType().ToString() & " abgerufen.")

        Return _buchungspositionenDataService
    End Function

    Public Function GetVertragDataService() As VertragsDataService

        If _vertragsDataService Is Nothing Then
            _vertragsDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.VertragServiceType), New Object() {_dataService}), 
                VertragsDataService)
        End If

        _ilog.DebugFormat(_vertragsDataService.GetType().ToString() & " abgerufen.")

        Return _vertragsDataService

    End Function

    Public Function GetStatusDataService() As StatusDataService

        If _statusDataService Is Nothing Then
            _statusDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.StatusServiceType), New Object() {_dataService}), 
                StatusDataService)
        End If

        _ilog.DebugFormat(_statusDataService.GetType().ToString() & " abgerufen.")

        Return _statusDataService

    End Function

    Public Function GetKontoauszugDataService() As KontoauszugDataService

        If _kontoauszugDataService Is Nothing Then
            _kontoauszugDataService = CType( _
                System.Activator.CreateInstance(_serviceAssembly.GetType(_config.KontoauszugServiceType), New Object() {_dataService}), 
                KontoauszugDataService)
        End If

        _ilog.Debug(_kontoauszugDataService.GetType().ToString() & " abgerufen.")

        Return _kontoauszugDataService

    End Function

#End Region



End Class
